<%@ page import="org.json.simple.*" %> <%--json_simple_1-1.jar--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import='java.sql.*'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
	//String str = request.getParameter("json배열이름");
	//System.out.println(str);
	/*JSONArray jarray = (JSONArray)JSONValue.parse(str);
	
	String userid = jarray.get(0);
	String userpass = jarray.get(1);*/
	
	String userid = request.getParameter("userid");
	String userpass = request.getParameter("userpass");

	String url = "jdbc:oracle:thin:@155.230.193.42:1521:oraknu";
	String user = "system";
	String pass = "DBLab012345";
	Connection con = null;
	String sql = null;
	int result;
	
	JSONObject resultJson = new JSONObject();


	try
	{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		System.out.println("success");
	}
	catch(ClassNotFoundException e)
	{
		System.err.println("error = " + e.getMessage());
		System.exit(1);
	}
	try
	{
		con = DriverManager.getConnection(url, user, pass);
	}
	catch(SQLException e)
	{
		System.err.println("sql error = " + e.getMessage());
		System.exit(1);
	}
	try
	{
		con.setAutoCommit(false);
		
		sql = "select id, pw, name from USERTABLE where id = ?";
		
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, userid);
		
		ResultSet rs = pstmt.executeQuery();
	
		if(rs.next())
		{
			String pw = rs.getString(2);
			if(pw.equals(userpass))
			{
				resultJson.put("result", true);
				resultJson.put("name", rs.getString("name"));

				session.setAttribute("logID", userid);
				//session.setAttribute("logPW", pw);
				session.setAttribute("sessionID", "ON");
				session.setMaxInactiveInterval(60*60);
			}
			else
			{
				session.invalidate();
			}
		}
		else
		{
			resultJson.put("result", false);
			resultJson.put("log", "ID 혹은 PW가 틀렸습니다.");
		}
		
		pstmt.close();
		con.close();
	}
	catch(Exception e)
	{
		System.err.println("sql error = " + e.getMessage());
	}
	
	Object o_sessionID =  session.getAttribute("sessionID");
	String sessionID = null;
	if(o_sessionID != null)
		sessionID = (String)o_sessionID;
	
	out.print(resultJson);

	if(sessionID != null)
	{
		if(sessionID.equals("ON"))
		{
			try
			{
				Class.forName("oracle.jdbc.driver.OracleDriver");
				//out.println("success");
				System.out.println(sessionID);
			}
			catch(ClassNotFoundException e)
			{
				System.err.println("error = " + e.getMessage());
				System.exit(1);
			}
			try
			{
				con = DriverManager.getConnection(url, user, pass);
			}
			catch(SQLException e)
			{
				System.err.println("sql error = " + e.getMessage());
				System.exit(1);
			}
		}
		else
		{
			session.invalidate();
			System.out.println("session삭제");
		}
	}
	else
	{
		System.out.println("세션없음");
	}

%>
